const db = require('../../utils/dbUtil');
const EventProxy = require('eventproxy');

class Menu {
    constructor() {
    }

    loadMenuCount() {
        return db.queryForInt('select count(1) from menu where disabled = 0;');
    }

    loadParentMenuList(){
        return db.query('select * from menu where disabled =0 and parent =0 and type = 1;');
    }

    loadMenuList(index, limit) {
        return db.query('select m1.*,m2.name as p_name from menu m1 left join menu as m2 on m1.parent = m2.id where m1.disabled =0 order by m1.parent,m1.`order`  limit  ?,?;', [index * limit, limit]);
    }

    loadMenuByUser(admin_id) {
        return db.query('select * from menu_auth join menu on menu_auth.menu_id = menu.id where menu_auth.admin_id = ? order by menu.parent,menu.`order`;', [admin_id]);
    }

    loadMenuById(menu_id){
        return db.query('select * from menu where id = ? ;',[menu_id]);
    }

    queryChildMenuCount(menu_id) {
        return db.queryForInt('select count(1) from menu where parent = ? and disabled = 0;', [menu_id])
    }

    queryMenuInUse(menu_id) {
        return db.queryForInt('select count(1) from menu_auth where menu_id = ?;', [menu_id])
    }

    addMenu(menu) {
        return db.insert('insert into menu (name,url,link,icon,parent,type,`order`,privilege,time) values(?,?,?,?,?,?,?,?,?);', [menu.name, menu.url, menu.link, menu.icon, menu.parent, menu.type, menu.order, menu.privilege, Date.now()]);
    }

    updateMenu(menu) {
        return db.update('update menu set name = ?,url=?,link = ?,icon= ?,parent= ?,type= ?,privilege = ?,`order` = ?,time = ? where id = ?;',
            [menu.name, menu.url, menu.link, menu.icon, menu.parent, menu.type, menu.privilege, menu.order, Date.now(), menu.id]);
    }

    deleteMenu(menu_id) {
        return db.update('update menu set disabled = 1 where id = ?;', [menu_id]);
    }

    authMenu(admin_id, menu_ids) {
        var promise = new Promise(function (resolve, reject) {
            db.beginTransaction().then(connection => {
                connection.query("delete from menu_auth where admin_id = ? ;", [admin_id], err => {
                    if (err) {
                        db.rollback(connection);
                        reject(err);
                    }


                    if (menu_ids.length > 0) {
                        var ep = new EventProxy();
                        ep.after('insert', menu_ids.length, function (list) {
                            db.commit(connection);
                            resolve(list);
                        });

                        ep.fail(function (err) {
                            db.rollback(connection);
                            reject(err);
                        });

                        menu_ids.forEach(id => {
                            connection.query('insert into menu_auth (menu_id,admin_id) values(?,?);', [id, admin_id], ep.done('insert'));
                        });
                    } else {
                        db.commit(connection);
                        resolve([]);
                    }


                });
            }).catch(err => reject(err));
        });
        return promise;
    }

}

Menu.type = {
    TYPE_MAIN : 0,
    TYPE_PARENT: 1,
    TYPE_LEAF: 2
}

module.exports = new Menu();